package com.example.star.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.Favorite;
import com.example.star.view.activity.PerimeterDetailActivity;
import com.example.star.view.setting.PersonalActivity;

import java.util.List;

public class FavoriteAdapter extends BaseAdapter {
    private static final String TAG = "FavoriteAdapter";
    private Context context;
    private List<Favorite> favorites;

    public FavoriteAdapter(Context context, List<Favorite> favorites) {
        this.context = context;
        this.favorites = favorites;
    }

    @Override
    public int getCount() {
        return favorites != null ? favorites.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return favorites != null ? favorites.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_favorite, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.imagePerimeter = convertView.findViewById(R.id.image_perimeter);
            viewHolder.starNameTextView = convertView.findViewById(R.id.star_name);
            viewHolder.perimeterTitleTextView = convertView.findViewById(R.id.perimeter_title);
            viewHolder.priceTextView = convertView.findViewById(R.id.prices);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Favorite favorite = favorites.get(position);
        //获取图片

        loadImage(viewHolder.imagePerimeter, favorite.getImageResource());
        viewHolder.starNameTextView.setText(favorite.getStarName());
        viewHolder.perimeterTitleTextView.setText(favorite.getTitle());
        viewHolder.priceTextView.setText(favorite.getPrices());

        // 添加点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PerimeterDetailActivity.class);
                intent.putExtra("starName", favorite.getStarName());
                intent.putExtra("perimeterTitle", favorite.getTitle());
                intent.putExtra("prices", favorite.getPrices());
                intent.putExtra("imageResource", favorite.getImageResource());
                intent.putExtra("productType", favorite.getProductType());
                intent.putExtra("freight", favorite.getFreight());
                intent.putExtra("deliveryAddress", favorite.getDeliveryAddress());
                context.startActivity(intent);
            }
        });

        return convertView;
    }
    private void loadImage(ImageView imageView, String imageResource) {
        if (imageResource != null) {
            try {
                int imageResId = context.getResources().getIdentifier(imageResource, "drawable", context.getPackageName());
                if (imageResId != 0) {
                    imageView.setImageResource(imageResId);
                } else {
                    Glide.with(context)
                            .load(imageResource)
                            .placeholder(R.drawable.register)
                            .error(R.drawable.register)
                            .into(imageView);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error loading image: " + e.getMessage());
                Glide.with(context)
                        .load(imageResource)
                        .placeholder(R.drawable.register)
                        .error(R.drawable.register)
                        .into(imageView);
            }
        }
    }

    private static class ViewHolder {
        ImageView imagePerimeter;
        TextView starNameTextView;
        TextView perimeterTitleTextView;
        TextView priceTextView;
    }

    public void clear() {
        favorites.clear();
        notifyDataSetChanged();
    }
}